<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="家长电话" prop="parentalPhone">
        <el-input
          v-model="queryParams.parentalPhone"
          placeholder="请输入家长电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="家长性别,对应字典表sys_dictionary中UserSex类型" prop="parentalSex">
        <el-select v-model="queryParams.parentalSex" placeholder="请选择家长性别,对应字典表sys_dictionary中UserSex类型" clearable size="small">
          <el-option
            v-for="dict in parentalSexOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="家长生日" prop="parentalBirthday">
        <el-date-picker clearable size="small"
          v-model="queryParams.parentalBirthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择家长生日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="家长身份证号" prop="parentalIdcard">
        <el-input
          v-model="queryParams.parentalIdcard"
          placeholder="请输入家长身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="家长学历,对应字典表sys_dictionary中EducationalType类型" prop="parentalEducational">
        <el-input
          v-model="queryParams.parentalEducational"
          placeholder="请输入家长学历,对应字典表sys_dictionary中EducationalType类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家长是否党员;关联sys_dictionary表中的IsNot类型" prop="parentalIscpc">
        <el-input
          v-model="queryParams.parentalIscpc"
          placeholder="请输入家长是否党员;关联sys_dictionary表中的IsNot类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家长工作单位" prop="parentalWorkunit">
        <el-input
          v-model="queryParams.parentalWorkunit"
          placeholder="请输入家长工作单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家长单位职务" prop="parentalPosition">
        <el-input
          v-model="queryParams.parentalPosition"
          placeholder="请输入家长单位职务"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家长单位职务" prop="createUser">
        <el-input
          v-model="queryParams.createUser"
          placeholder="请输入家长单位职务"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家长单位职务" prop="updateUser">
        <el-input
          v-model="queryParams.updateUser"
          placeholder="请输入家长单位职务"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tongban:parental:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tongban:parental:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tongban:parentalChild:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tongban:parentalChild:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="parentalList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="家长id" align="center" prop="parentalId" />-->
      <el-table-column label="用户名" align="center" prop="memberAccount" />
      <el-table-column label="用户昵称" align="center" prop="memberNick" />
      <el-table-column label="角色名称" align="center" prop="parentRoleName" />
      <el-table-column label="联系方式" align="center" prop="parentalPhone" />
      <el-table-column label="家长生日" align="center" prop="parentalBirthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.parentalBirthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="parentalSex" :formatter="sexTypeFormat"/>
      <el-table-column label="工作单位" align="center" prop="parentalWorkunit" />
      <el-table-column label="机构名称" align="center" prop="institutionName" />
      <el-table-column label="年级名称" align="center" prop="gradeName" />
      <el-table-column label="班级名称" align="center" prop="className" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tongban:parentalChild:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tongban:parentalChild:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改家长 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="用户名" prop="memberAccount">
          <el-input v-model=" form.memberAccount" placeholder="请输入用户名"/>
        </el-form-item>

        <el-form-item label="用户昵称" prop="memberNick">
          <el-input v-model=" form.memberNick" placeholder="请输入用户昵称"/>
        </el-form-item>
        <el-form-item label="家长性别" prop="parentalSex">
          <el-select v-model="form.parentalSex" placeholder="请选择家长性别">
            <el-option
              v-for="dict in sexTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="家长生日" prop="parentalBirthday">
          <el-date-picker clearable size="small"
            v-model="form.parentalBirthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择家长生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="家长身份证号" prop="parentalIdcard">
          <el-input v-model="form.parentalIdcard" placeholder="请输入家长身份证号" />
        </el-form-item>
        <el-form-item label="家长电话" prop="parentalPhone">
          <el-input v-model="form.parentalPhone" placeholder="请输入家长电话" />
        </el-form-item>
<!--        <el-form-item label="家长是否党员" prop="parentalIscpc">
          <el-input v-model="form.parentalIscpc" placeholder="请输入家长是否党员" />
        </el-form-item>-->
        <el-form-item label="家长工作单位" prop="parentalWorkunit">
          <el-input v-model="form.parentalWorkunit" placeholder="请输入家长工作单位" />
        </el-form-item>
        <el-form-item label="家长单位职务" prop="parentalPosition">
          <el-input v-model="form.parentalPosition" placeholder="请输入家长单位职务" />
        </el-form-item>
<!--        <el-form-item label="创建用户" prop="createUser">
          <el-input v-model="form.createUser" placeholder="请输入家长单位职务" />
        </el-form-item>
        <el-form-item label="更新用户" prop="updateUser">
          <el-input v-model="form.updateUser" placeholder="请输入家长单位职务" />
        </el-form-item>-->

        <el-form-item label="机构名称" prop="institutionId" v-if="deptList">
          <el-select v-model="form.institutionId" placeholder="请选择机构" clearable size="small" @change="selectGrade">
            <el-option
              v-for="dept in deptList"
              :key="dept.institutionId"
              :label="dept.institutionName"
              :value="dept.institutionId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="年级名称" prop="gradeId" v-if="gradeList">
          <el-select v-model="form.gradeId" placeholder="请选择年级" clearable size="small" @change="selectClass">
            <el-option
              v-for="dept in gradeList"
              :key="dept.gradeId"
              :label="dept.gradeName"
              :value="dept.gradeId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="班级名称" prop="classId" v-if="classList">
          <el-select v-model="form.classId" placeholder="请选择班级" clearable size="small" @change="selectChild">
            <el-option
              v-for="dept in classList"
              :key="dept.classId"
              :label="dept.className"
              :value="dept.classId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="孩子名称" prop="childId" v-if="childList">
          <el-select v-model="form.childId" placeholder="请选择孩子" clearable size="small">
            <el-option
              v-for="dept in childList"
              :key="dept.childId"
              :label="dept.childName"
              :value="dept.childId"
            />
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listParental, getParental, delParental, addParental, updateParental, exportParental } from "@/api/tongban/parental";
import {listInstitutionData} from "@/api/tongban/institution";
import {listInstitutionGrade} from "@/api/tongban/grade";
import {listInstitutionClass} from "@/api/tongban/class";
import {listInstitutionChild} from "@/api/tongban/child";

export default {
  name: "Parental",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 家长,关联member,为家长的扩展信息表格数据
      parentalList: [],
      deptList: [],
      gradeList: [],
      classList: [],
      childList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 家长性别,对应字典表sys_dictionary中UserSex类型字典
      sexTypeOptions: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentalSex: null,
        parentalBirthday: null,
        parentalIdcard: null,
        parentalPhone: null,
        parentalEducational: null,
        parentalIscpc: null,
        parentalWorkunit: null,
        parentalPosition: null,
        createUser: null,
        updateUser: null,
        childId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentalSex: [
          { required: true, message: "家长性别", trigger: "change" }
        ],
        /*createTime: [
          { required: true, message: "家长性别", trigger: "blur" }
        ],
        createUser: [
          { required: true, message: "家长性别", trigger: "blur" }
        ],*/
        parentalPhone: [
          { required: true, message: "家长电话",trigger: "blur" }
        ],
        memberAccount: [
          { required : true, message : "登录账号",trigger : "blur" }
        ],
        memberNick: [
          {required : true, message : "用户昵称",trigger : "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sex_type").then(response => {
      this.sexTypeOptions = response.data;
    });

    listInstitutionData().then(response => {
      this.deptList = response.data;
    });
  },
  methods: {
    /** 查询家长 */
    getList() {
      this.loading = true;
      listParental(this.queryParams).then(response => {
        this.parentalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询年级*/
    selectGrade(id){
      listInstitutionGrade(id).then(res =>{
        this.gradeList = res.data;
      })
    },

    /** 查询班级*/
    selectClass(id){
      listInstitutionClass(id).then(response => {
        this.classList = response.data;
      })
    },

    /** 查询孩子*/
    selectChild(id){
      listInstitutionChild(id).then(response => {
          this.childList = response.data;
      })
    },

    // 家长性别字典翻译
    sexTypeFormat(row, column) {
      return this.selectDictLabel(this.sexTypeOptions, row.parentalSex);
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        memberNick: null,
        memberAccount: null,
        parentalId: null,
        parentalSex: null,
        parentalBirthday: null,
        parentalIdcard: null,
        parentalPhone: null,
        parentalEducational: null,
        parentalIscpc: null,
        parentalWorkunit: null,
        parentalPosition: null,
        createTime: null,
        createUser: null,
        updateTime: null,
        updateUser: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.parentalId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加家长";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const parentalId = row.parentalId || this.ids
      getParental(parentalId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改家长";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.parentalId != null) {
            updateParental(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addParental(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const parentalIds = row.parentalId || this.ids;
      this.$confirm('是否确认删除家长,关联member,为家长的扩展信息编号为"' + parentalIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delParental(parentalIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有家长,关联member,为家长的扩展信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportParental(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
